package com.mazong.serverblog.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.xml.transform.Result;
import java.io.IOException;

@ControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    //空指针异常
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public String nullPointerExceptionHandler(NullPointerException ex) {
        //ex.printStackTrace();
        logger.error("NullPointerException捕获了", ex);
        return "NullPointerException捕获了";
    }

    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public String classCastExceptionHandler(ClassCastException ex) {
        ex.printStackTrace();
        return "ClassCastException捕获了";
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public String iOExceptionHandler(IOException ex) {
        ex.printStackTrace();
        return "IOException捕获了";
    }

    //未知方法异常
    @ExceptionHandler(NoSuchMethodException.class)
    @ResponseBody
    public String noSuchMethodExceptionHandler(NoSuchMethodException ex) {
        ex.printStackTrace();
        return "NoSuchMethodException捕获了";
    }

    //数组越界异常
    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public String indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
        ex.printStackTrace();
        return "IndexOutOfBoundsException捕获了";
    }

    //运行时异常
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public String runtimeExceptionHandler(RuntimeException runtimeException) {
        runtimeException.printStackTrace();
        return "RuntimeException捕获了";
    }

    /**
     *            Exception                    HTTP Status Code
     * ConversionNotSupportedException         500 (Internal Server Error)
     * HttpMessageNotWritableException         500 (Internal Server Error)
     * HttpMediaTypeNotSupportedException      415 (Unsupported Media Type)
     * HttpMediaTypeNotAcceptableException     406 (Not Acceptable)
     * HttpRequestMethodNotSupportedException  405 (Method Not Allowed)
     * NoSuchRequestHandlingMethodException    404 (Not Found)
     * TypeMismatchException                   400 (Bad Request)
     * HttpMessageNotReadableException         400 (Bad Request)
     * MissingServletRequestParameterException 400 (Bad Request)
     *
     * @param ex
     * @return
     */

    //400错误
    @ExceptionHandler({HttpMessageNotReadableException.class})
    @ResponseBody
    public String requestNotReadable(HttpMessageNotReadableException ex){
        System.out.println("400..requestNotReadable");
        ex.printStackTrace();
        return "HttpMessageNotReadableException捕获了";
    }

    //400错误
    @ExceptionHandler({TypeMismatchException.class})
    @ResponseBody
    public String requestTypeMismatch(TypeMismatchException ex){
        System.out.println("400..TypeMismatchException");
        ex.printStackTrace();
        return "TypeMismatchException捕获了";
    }
    //400错误
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseBody
    public String requestMissingServletRequest(MissingServletRequestParameterException ex){
        System.out.println("400..MissingServletRequest");
        ex.printStackTrace();
        return "MissingServletRequestParameterException捕获了";
    }
    //405错误
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseBody
    public String request405(HttpRequestMethodNotSupportedException ex){
        System.out.println("405...");
        ex.printStackTrace();
        return "HttpRequestMethodNotSupportedException捕获了";
    }
    //406错误
    @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
    @ResponseBody
    public String request406(HttpMediaTypeNotAcceptableException ex){
        System.out.println("404...");
        ex.printStackTrace();
        return "HttpMediaTypeNotAcceptableException捕获了";
    }
    //500错误
    @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
    @ResponseBody
    public String server500(RuntimeException ex){
        System.out.println("500...");
        ex.printStackTrace();
        return "ConversionNotSupportedException捕获了";
    }

    //404错误
    @ExceptionHandler(value = NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handlerNoHandlerFoundException(NoHandlerFoundException ex) {
        System.out.println("404...");
        ex.printStackTrace();
        return "NoHandlerFoundException捕获了";
    }

    @ExceptionHandler(value = Exception.class)
    public String handlerException(Exception ex) {
        System.out.println("404...");
        ex.printStackTrace();
        return "NoHandlerFoundException捕获了";
    }
}
